package com.zmx.patients.view.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.patients.BaseActivity;
import com.zmx.patients.R;
import com.zmx.patients.adapter.ArticleMessageAdapter;
import com.zmx.patients.adapter.RecommendedDoctorAdapter;
import com.zmx.patients.fragment.HomeFragment;
import com.zmx.patients.okhttp.OkHttp3ClientManager;
import com.zmx.patients.pojo.ArticleMessage;
import com.zmx.patients.pojo.Doctor;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *时间：2020/7/10 13:02
 *功能：心灵之窗
 */
public class WindowOfSoulActivity extends BaseActivity {

    private RecommendedDoctorAdapter rdadapter;
    private List<Doctor> doctors;
    private RecyclerView recycler_view,re_view;

    private ArticleMessageAdapter adapter;
    private List<ArticleMessage> lists;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_window_of_soul;
    }

    @Override
    protected void initViews() {

        setToolbar(R.id.tool_bar);

        recycler_view = findViewById(R.id.recycler_view);
        re_view = findViewById(R.id.re_view);
        doctors = new ArrayList<>();
        rdadapter = new RecommendedDoctorAdapter(this);
        recycler_view.setLayoutManager(new GridLayoutManager(mActivity, 3));
        recycler_view.setAdapter(rdadapter);
        rdadapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

                String did = doctors.get(listPosition).getMember_id() + "";
                //目前Activity→目标Activity
                Intent intent = new Intent(mActivity, DoctorDetailsActivity.class);
                intent.putExtra("doctor_id", did);
                intent.putExtra("class_id", doctors.get(listPosition).getClass_id() + "");
                intent.putExtra("hosp_id", doctors.get(listPosition).getHosp_id() + "");
                startActivity(intent);

            }
        });

        re_view = findViewById(R.id.re_view);
        adapter = new ArticleMessageAdapter(this);
        lists = new ArrayList<>();
        re_view.setLayoutManager(new LinearLayoutManager(mActivity));
        re_view.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

                Intent intent = new Intent(mActivity, ArticleDetailsActivity.class);
                intent.putExtra("am", lists.get(listPosition));
                startActivity(intent);

            }
        });

        getAllDoctor();
        getArticle();
    }


    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:
                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        JSONArray array = object.getJSONArray("data");

                        Gson gson = new Gson();
                        for (int i = 0; i < array.length(); i++) {

                            JSONObject jsonObject = array.getJSONObject(i);
                            ArticleMessage articleMessage = gson.fromJson(jsonObject.toString(), ArticleMessage.class);
                            lists.add(articleMessage);
                        }
                        adapter.setListAndNotifyDataSetChanged(lists);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;

                case 2:
                    doctors.clear();

                    Log.e("内容",""+msg.obj.toString());
                    JSONObject jsonObject = null;


                    try {
                        jsonObject = new JSONObject(msg.obj.toString());
                        JSONArray array = jsonObject.getJSONArray("data");

                        Gson g = new Gson();
                        for (int i = 0; i < array.length(); i++) {

                            JSONObject object = array.getJSONObject(i);
                            Doctor c = g.fromJson(object.toString(), Doctor.class);
                            doctors.add(c);

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    rdadapter.setListAndNotifyDataSetChanged(doctors);//就是设置list然后刷新

                    break;
                case 3:

            }

        }
    };


    public void getAllDoctor() {

//        List<String> l = new ArrayList<>();
//        l.add(5+"");
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
//        params.put("sign", AllUtil.md5(sing(l)));
//        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
//        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
//        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getAllDoctor");
        params.put("data[class_id]", 5+"");
        params.put("data[row]",1+"");
        params.put("data[number]",6+"");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/api/", params, handler, 2, 404);

    }

    public void getArticle() {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("data[row]",1+"");
        params.put("data[number]",10+"");
        params.put("data[column_id]",7);
        params.put("cmd","getArticle");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/article/", params, handler, 1, 404);

    }

}

